Partial Public Class pages_Orders_usercontrols_UCOrders : Inherits UserControl

#Region "Events"
    Public Event ExportClick()
    Public Event ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer)
#End Region

#Region "Interface"
    Public WriteOnly Property TotalValue As Decimal
        Set(value As Decimal)
            litTotalValue.Text = CTextbox.SetMoney(value)
        End Set
    End Property
    Public WriteOnly Property TotalPaid As Decimal
        Set(value As Decimal)
            litTotalPaid.Text = CTextbox.SetMoney(value)
        End Set
    End Property
    Public WriteOnly Property TotalNet As Decimal
        Set(value As Decimal)
            litTotalNet.Text = CTextbox.SetMoney(value)
        End Set
    End Property
    Public WriteOnly Property Percent As Double
        Set(value As Double)
            litPercent.Text = CTextbox.SetPercent(value, "n0")
            litPercent.ToolTip = CTextbox.SetPercent(value, "n3")
        End Set
    End Property


    Public Sub Display(ByVal orders As COrderList, hideCustomer As Boolean, hideProduct As Boolean) 'Short list due to sql-based paging (by exposing Me.Info)
        colNumber.Visible = orders.Count > 0
        colProduct.Visible = Not hideProduct
        colProduct2.Visible = Not hideProduct
        colCustomer.Visible = Not hideCustomer
        If Not colNumber.Visible Then col1.ColSpan -= 1
        If Not colProduct.Visible Then col1.ColSpan -= 1
        If Not colCustomer.Visible Then col1.ColSpan -= 1
        For Each i As COrder In orders
            UCOrder(plh).Display(i, orders, Me.PagingInfo, hideCustomer, hideProduct)
        Next

        Dim donTot As Double = New CPayment().SelectSum("PaymentAmount", Nothing, New CCriteriaList("PaymentOrderId", Integer.MinValue))
        Dim donNet As Double = New CPayment().SelectSum("PaymentAmount-PaymentFee", Nothing, New CCriteriaList("PaymentOrderId", Integer.MinValue))
        litDonationsTotal.Text = CTextbox.SetMoney(donTot)
        litDonationsNet.Text = CTextbox.SetMoney(donNet)
    End Sub
    Public ReadOnly Property PagingInfo() As CPagingInfo
        Get
            Return ctrlPaging.Info
        End Get
    End Property
#End Region

#Region "User Controls"
    Private Shared Function UCOrder(ByVal target As Control) As pages_orders_usercontrols_UCOrder
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCOrder)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_orders_usercontrols_UCOrder)
    End Function
#End Region

#Region "Event Handlers"
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click
        RaiseEvent ExportClick()
    End Sub
    Protected Sub btnResort_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnSortByProduct.Click, btnSortByCustomer.Click, btnSortByOrderPriceHistorical.Click, btnSortByIsPa.Click, btnSortByOrderCreated.Click
        'Toggle descending if necessary
        Dim sortBy As String = CType(sender, LinkButton).CommandArgument
        Dim descending As Boolean = ctrlPaging.IsDescending
        Dim currentSort As String = ctrlPaging.SortColumn
        If Not String.IsNullOrEmpty(currentSort) Then
            If currentSort = sortBy Then descending = Not descending
        End If

        'Bubble up as event, search page will add filter info and redirect
        RaiseEvent ResortClick(sortBy, descending, ctrlPaging.Info.PageIndex + 1)
    End Sub
#End Region

#Region "Paging"
    Public ReadOnly Property Info() As CPagingInfo
        Get
            Return ctrlPaging.Info
        End Get
    End Property
    Public Property QueryString() As String
        Get
            Return ctrlPaging.QueryString
        End Get
        Set(ByVal value As String)
            ctrlPaging.QueryString = value
        End Set
    End Property
    Public Property PageSize() As Integer
        Get
            Return ctrlPaging.PageSize
        End Get
        Set(ByVal value As Integer)
            ctrlPaging.PageSize = value
        End Set
    End Property
#End Region

End Class
